/**
 * 
 */
package com.zznode.client.wlanpolling.test;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.zznode.client.wlanpolling.R;

/**
 * 
 *
 */
public class WLANPollingTabActivity extends TabActivity {

	public static final String ID_TAB_RESCONFIRM = "resconfirm";
	public static final String ID_TAB_WIRELESS_EVN = "wirelessenv";
	public static final String ID_TAB_POLLINGTEST = "pollingtest";
	public static final String ID_TAB_SETTING = "result";
	
	private TextView tvTitle;

	private int[] tabInactiveRes = new int[] { R.drawable.menu_confirm,
			R.drawable.menu_wifisetting, R.drawable.menu_test,
			R.drawable.menu_setting };
	private int[] tabActiveRes = new int[] { R.drawable.menu_confirm_active,
			R.drawable.menu_wifisetting_active, R.drawable.menu_test_active,
			R.drawable.menu_setting_active };

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// 定制TitleBar
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.pollingtab);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.titlebar);
		tvTitle = (TextView) findViewById(R.id.windowtitle);

		addTab("资源确认", ID_TAB_RESCONFIRM, tabInactiveRes[0],
				ResConfirmActivity.class);
		addTab("无线环境", ID_TAB_WIRELESS_EVN, tabInactiveRes[1],
				WirelessEnvActivity.class);
		addTab("巡检测试", ID_TAB_POLLINGTEST, tabInactiveRes[2],
				PollTestActivity.class);
		addTab("巡检结果", ID_TAB_SETTING, tabInactiveRes[3], PollTestResultActivity.class);

		final TabHost tabHost = getTabHost();
		tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
			@Override
			public void onTabChanged(String tabId) {
				if (tabId.equals(ID_TAB_RESCONFIRM)) {
					tvTitle.setText("WiFi现场例行巡检-资源确认");
					toggleNavButton(true);
				} else if (tabId.equals(ID_TAB_WIRELESS_EVN)) {
					tvTitle.setText("WiFi现场例行巡检-无线环境");
					toggleNavButton(true);
				} else if (tabId.equals(ID_TAB_POLLINGTEST)) {
					tvTitle.setText("WiFi现场例行巡检-巡检测试");
					toggleNavButton(true);
				} else if (tabId.equals(ID_TAB_SETTING)) {
					tvTitle.setText("WiFi现场例行巡检-测试结果");
					toggleNavButton(false);
				}

				for (int i = 0; i <= 3; i++) {
					View view = getTabHost().getTabWidget()
							.getChildTabViewAt(i);
					view.setBackgroundResource(0);
					if (view != tabHost.getCurrentTabView()) {
						// set inactive
						((ImageView) view.findViewById(R.id.icon))
								.setImageResource(tabInactiveRes[i]);
					} else {
						// set active
						((ImageView) view.findViewById(R.id.icon))
								.setImageResource(tabActiveRes[i]);
					}
				}

				getTabHost().getCurrentTabView().setBackgroundResource(
						R.drawable.menu_activebg02);

			}
		});

		tabHost.setCurrentTab(0);
		tvTitle.setText("WiFi现场例行巡检-资源确认");
		
		// 前后导航
		Button btnPrev = (Button) findViewById(R.id.prev);
		Button btnNext = (Button) findViewById(R.id.next);

		btnPrev.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				int tabIndex = tabHost.getCurrentTab();
				if (tabIndex > 0) {
					tabIndex--;
					tabHost.setCurrentTab(tabIndex);
				}
			}
		});

		btnNext.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				int tabIndex = tabHost.getCurrentTab();
				if (tabIndex < 3) {
					tabIndex++;
					tabHost.setCurrentTab(tabIndex);
				}
			}
		});
		
	}

	private void toggleNavButton(boolean flag) {
		
		Button btnPrev = (Button) findViewById(R.id.prev);
		Button btnNext = (Button) findViewById(R.id.next);

		if (flag) {
			btnPrev.setVisibility(View.VISIBLE);
			btnNext.setVisibility(View.VISIBLE);
		} else {
			btnPrev.setVisibility(View.INVISIBLE);
			btnNext.setVisibility(View.INVISIBLE);
		}
		
	}

	private void addTab(String label, String tag, int drawableId, Class<?> c) {

		TabHost tabHost = getTabHost();
		Intent intent = new Intent(this, c);
		TabHost.TabSpec spec = tabHost.newTabSpec(tag);

		View tabIndicator = LayoutInflater.from(this).inflate(
				R.layout.tab_indicator, getTabWidget(), false);
		ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
		icon.setImageResource(drawableId);

		TextView title = (TextView) tabIndicator.findViewById(R.id.title);
		title.setText(label);

		if (tag.equals(ID_TAB_RESCONFIRM)) {
			tabIndicator.setBackgroundResource(R.drawable.menu_activebg02);
			icon.setImageResource(R.drawable.menu_confirm_active);
		} else {
			tabIndicator.setBackgroundResource(0);
		}

		spec.setIndicator(tabIndicator);
		spec.setContent(intent);
		tabHost.addTab(spec);
	}

	public void selectNextTab() {
		TabHost tabHost = getTabHost();
		int tabIndex = tabHost.getCurrentTab();
		if (tabIndex < 3) {
			tabIndex++;
			tabHost.setCurrentTab(tabIndex);
		}
	}

	public void selectPrevTab() {
		TabHost tabHost = getTabHost();
		int tabIndex = tabHost.getCurrentTab();
		if (tabIndex > 0) {
			tabIndex--;
			tabHost.setCurrentTab(tabIndex);
		}
	}
}
